//
//  Signal.m
//  AffichageSignal
//
//  Created by Pierre DUCHENE on 09/02/10.
//  Copyright 2010 Pierre DUCHENE. All rights reserved.
//

#import "Signal.h"

#import "SynthKit.h"
#import "AudioWrapper.h"

@implementation Signal

@synthesize values;

- (id) init
{
	self = [super init];
	if (self != nil) {
		values = [[NSArray alloc] init];
	}
	return self;
}

- (void) dealloc
{
	[values release];
	values = nil;
	
	[super dealloc];
}

- (void)addSample:(NSArray*)aSample
{
	NSMutableArray* temp = [NSMutableArray arrayWithArray:values];
	/*for (int i = 0; i < [aSample count] && i < [values count]; i++) {
		[temp removeObjectAtIndex:0];
	}*/
	[temp addObjectsFromArray:aSample];
	/*while ([temp count] > kDefaultSampleSize) {
		[temp removeObjectAtIndex:0];
	}*/
	[values release];
	values = [temp retain];
}

- (void) updateAudioSignal{
	NSMutableArray* temp = [[NSMutableArray alloc] init];
	NSArray* buffer = [[AudioWrapper sharedWrapper] buffer];
	NSInteger outputIndex = [[AudioWrapper sharedWrapper] bufferOutputIndex];
	NSInteger shownElementsNumber = kDefaultSampleSize*kNbSampleShown;
	for (int i = 0; i < shownElementsNumber; i++) {
		NSInteger index = ((outputIndex-i)+(kBufferSize))%kBufferSize;
		if (index<0) {
			NSLog(@"%@%i",@"index = ",index);
		}
		[temp insertObject:[buffer objectAtIndex:index] atIndex:0];
	}
	[values release];
	values = temp;
}

@end
